<template>
  <el-dialog :title="'订单详情'" :model-value="dialog" @close="close" width="50%">
    <div v-if="loading" class="loading-container">
      <el-loading :model="loading" text="加载中..." />
    </div>
    <div v-else-if="!detailData" class="empty-container">
      <el-empty description="暂无数据" />
    </div>
    <div v-else class="detail-container">
      <!-- 订单信息 -->
      <el-card class="mb-4">
        <template #header>
          <div class="card-header">
            <span>订单信息</span>
          </div>
        </template>
        <el-descriptions border :column="2">
          <el-descriptions-item label="订单号">{{ detailData.orderNumber }}</el-descriptions-item>
          <el-descriptions-item label="订单状态">{{ orderStatusMap.get(detailData.orderState) }}</el-descriptions-item>
          <el-descriptions-item label="确认到达上车点">
            {{ formatTime(detailData.confirmArrivalPickUpPointTime) }}</el-descriptions-item>
          <el-descriptions-item label="接单状态">
            {{ detailData.acceptOrderState == 0 ? '未接单' : '已接单' }}</el-descriptions-item>
          <el-descriptions-item label="订单类型">{{ orderTypeMap.get(detailData.orderType) }}</el-descriptions-item>
          <el-descriptions-item label="车型类型">{{ priceModelTypeMap.get(detailData.modelType) }}</el-descriptions-item>
          <el-descriptions-item label="价格" v-if="userStore.accountType == 1 || userStore.accountType == 3">
            {{ detailData.price }}
          </el-descriptions-item>
          <el-descriptions-item label="司机价格" v-if="userStore.accountType == 1">
            {{ detailData.driverShowPrice }}
          </el-descriptions-item>
          <el-descriptions-item label="包车小时数" v-if="detailData.hours">{{ detailData.hours }}</el-descriptions-item>
          <el-descriptions-item label="订单来源">{{ detailData.oderSourceName }}</el-descriptions-item>
          <el-descriptions-item label="乘客姓名">{{ detailData.passengerName }}</el-descriptions-item>
          <el-descriptions-item label="颜色">{{ detailData.color }}</el-descriptions-item>
          <el-descriptions-item label="乘客手机号">{{ detailData.passengerMobile }}</el-descriptions-item>
          <el-descriptions-item label="用车方">{{ detailData.useCarPartnerName }}</el-descriptions-item>
          <el-descriptions-item label="出发地-目的地" v-if="detailData.departureAddress">
            {{ detailData.departureAddress }}-{{ detailData.destinationAddress }}
          </el-descriptions-item>
          <el-descriptions-item label="路线" v-if="detailData.route">
            {{ detailData.route }}
          </el-descriptions-item>
          <el-descriptions-item label="航班号" v-if="detailData.flightNumber">
            {{ detailData.flightNumber }}
          </el-descriptions-item>
          <el-descriptions-item label="车次号" v-if="detailData.trainNumber">
            {{ detailData.trainNumber }}
          </el-descriptions-item>
          <el-descriptions-item label="城市" v-if="detailData.province">
            {{ detailData.province }}-{{ detailData.city }}
          </el-descriptions-item>
          <el-descriptions-item label="订单服务开始时间">{{ formatTime(detailData.serviceStartTime) }}</el-descriptions-item>
          <el-descriptions-item label="司机确认接单时间">
            {{ formatTime(detailData.confirmAcceptOrderTime) }}
          </el-descriptions-item>
          <el-descriptions-item label="司机开始行程时间">{{ formatTime(detailData.startRouteTime) }}</el-descriptions-item>
          <el-descriptions-item label="行程结束时间">{{ formatTime(detailData.serviceEndTime) }}</el-descriptions-item>
          <!--  -->
          <el-descriptions-item label="到达上车点">
            <el-image style="width: 100px; height: 100px; margin-right: 5px;"
              :src="'http://8.155.170.55' + item.filePath"
              v-for="item in detailData.fileResponseList?.filter((x: any) => x.orderNode == 1)"
              :preview-src-list="detailData.fileResponseList?.filter((x: any) => x.orderNode == 1).map((x: any) => 'http://8.155.170.55' + x.filePath)"
              fit="contain">
            </el-image>
          </el-descriptions-item>
          <el-descriptions-item label="到达目的地">
            <el-image style="width: 100px; height: 100px; margin-right: 5px;"
              :src="'http://8.155.170.55' + item.filePath"
              v-for="item in detailData.fileResponseList?.filter((x: any) => x.orderNode != 1)"
              :preview-src-list="detailData.fileResponseList?.filter((x: any) => x.orderNode != 1).map((x: any) => 'http://8.155.170.55' + x.filePath)"
              fit="contain">
            </el-image>
          </el-descriptions-item>

          <el-descriptions-item label="备注">{{ detailData.remark || '-' }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ formatTime(detailData.createTime) }}</el-descriptions-item>
        </el-descriptions>
      </el-card>
      <!-- 车辆信息 -->
      <el-card class="mb-4">
        <template #header>
          <div class="card-header">
            <span>车辆信息</span>
          </div>
        </template>
        <el-descriptions border :column="2">
          <el-descriptions-item label="车架号（vin）">{{ detailData.vin }}</el-descriptions-item>
          <el-descriptions-item label="车牌号">{{ detailData.plateNumber }}</el-descriptions-item>
          <el-descriptions-item label="品牌">{{ detailData.brandName }}</el-descriptions-item>
          <el-descriptions-item label="车系">{{ detailData.seriesName }}</el-descriptions-item>
          <el-descriptions-item label="车型">{{ detailData.modelName }}</el-descriptions-item>
        </el-descriptions>
      </el-card>
      <!-- 司机信息 -->
      <el-card>
        <template #header>
          <div class="card-header">
            <span>司机信息</span>
          </div>
        </template>
        <el-descriptions border :column="2" v-if="driverInfo">
          <el-descriptions-item label="姓名">{{ driverInfo.name || '-' }}</el-descriptions-item>
          <el-descriptions-item label="年龄">{{ driverInfo.age || '-' }}</el-descriptions-item>
          <el-descriptions-item label="性别">{{ driverInfo.sex == 1 ? '女' : '男'
          }}</el-descriptions-item>
          <el-descriptions-item label="手机号">{{ driverInfo.mobileNumber || '-' }}</el-descriptions-item>
          <el-descriptions-item label="紧急联系人">{{ driverInfo.emergencyContactName || '-' }}</el-descriptions-item>
          <el-descriptions-item label="紧急联系人手机号">{{ driverInfo.emergencyContactPhone || '-'
          }}</el-descriptions-item>
        </el-descriptions>
      </el-card>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { formatTime } from '@/utils'
import { useUserStore } from '@/stores/user'
import { queryOrderDetail } from '@/api/order'
import { orderStatusMap, orderTypeMap, priceModelTypeMap } from '@/config/constants'

const props = defineProps<{
  dialog: boolean
  currentForm: {
    orderUuid?: string
  }
}>()

const emit = defineEmits<{
  'update:dialog': [value: boolean]
  'update:currentForm': [value: object]
}>()

const userStore = useUserStore()
const loading = ref(false)
const detailData = ref<any>(null)
const driverInfo = ref<any>(null)

const close = () => {
  emit('update:dialog', false)
  // 只关闭弹窗并清理内部数据，不再清空父组件的currentForm
  detailData.value = null
}

const loadDetail = () => {
  console.log(11)
  if (!props.currentForm.orderUuid) {
    ElMessage.error('订单ID不存在')
    return
  }

  loading.value = true
  queryOrderDetail({ orderUuid: props.currentForm.orderUuid }).then((res: any) => {
    if (res.code === 200) {
      detailData.value = res.data.data.orderSnapshotResponse
      driverInfo.value = res.data.data.driverResponse
    } else {
      ElMessage.error(res.message || '获取订单详情失败')
    }
  }).catch(() => {
    ElMessage.error('获取订单详情失败')
  }).finally(() => {
    loading.value = false
  })
}

watch(() => props.dialog, (newVal) => {
  console.log(1111, props.currentForm)
  if (newVal && props.currentForm.orderUuid) {
    loadDetail()
  }
})

onMounted(() => {
  if (props.dialog && props.currentForm.orderUuid) {
    loadDetail()
  }
})
</script>

<style scoped>
.detail-container {
  max-height: 400px;
  overflow-y: auto;
}

.mb-4 {
  margin-bottom: 16px;
}

.card-header {
  font-weight: bold;
}

.loading-container,
.empty-container {
  min-height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 让el-descriptions组件的标签列和内容列等宽 */
/* :deep(.el-descriptions__label) {
  width: 50% !important;
  flex: 0 0 50% !important;
} */

:deep(.el-descriptions__content) {
  width: 30% !important;
  flex: 0 0 50% !important;
}
</style>
